package com.ruoyi.backend.service.Impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.backendEnum.FieldException;
import com.ruoyi.backend.mapper.FieldMapper;
import com.ruoyi.backend.pojo.Field;
import com.ruoyi.backend.service.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FieldServiceImpl implements FieldService {

    @Autowired
    private FieldMapper fieldMapper;

    @Override
    public Field upload(Field field) {
        fieldMapper.upload(field);
        return field;
    }

    @Override
    public void delete(Integer investigateId) {
        if(fieldMapper.getCountId(investigateId) == 0){
            throw new ServiceException(FieldException.FIELD_DATA_NOT_EXIST);
        }
        fieldMapper.delete(investigateId);
    }

    @Override
    public void update(Field field) {
        if(fieldMapper.getCountId(field.getInvestigateId()) == 0){
            throw new ServiceException(FieldException.FIELD_DATA_NOT_EXIST);
        }
        fieldMapper.update(field);
    }

    @Override
    public List<Field> search(String mineId, String investigateType, String investigateName, String investigateUnit) {
        List<Field> fieldList = fieldMapper.search(mineId, investigateType, investigateName, investigateUnit);
//        if(fieldList.isEmpty()){
//            throw new ServiceException(FieldException.FIELD_DATA_NOT_FOUND);
//        }
        return fieldList;
    }

    @Override
    public List<Field> searchAll() {
        return fieldMapper.searchAll();
    }
}
